package stark.app.function.poster;

import android.content.Intent;
import android.net.Uri;

import com.bumptech.glide.Glide;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;
import com.stark.poster.PosterEntry;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityPosterDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.constant.ThemeMode;

/**
 * Author：daizhongbin on 2021/4/1
 * Email：1027154338@qq.com
 * Desc:
 */
public class PosterDemoActivity extends BaseNoModelActivity<ActivityPosterDemoBinding> {

    private static final int REQ_POSTER = 1;

    @Override
    protected int onCreate() {
        return R.layout.activity_poster_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnPosterNight.setOnClickListener(v -> {
            startPoster(paths -> {
                PosterEntry.enterPosterMake(this, paths);
            });
        });
        mDataBinding.btnPosterLight.setOnClickListener(v -> {
            startPoster(paths -> {
                PosterEntry.enterPosterMake(this, ThemeMode.LIGHT, paths);
            });
        });
        mDataBinding.btnPosterRet.setOnClickListener(v -> {
            startPoster(paths -> {
                PosterEntry.enterPosterMakeForRet(this, paths, REQ_POSTER);
            });
        });
        mDataBinding.btnPosterCallback.setOnClickListener(v -> {
            startPoster(paths -> {
                PosterEntry.enterPosterMakeWithCallback(this, paths, bitmap -> {
                    mDataBinding.ivRet.setImageBitmap(bitmap);
                });
            });
        });
        mDataBinding.btnPosterCustom.setOnClickListener(v -> {
            startPoster(paths -> {
                SubPosterMakeActivity.start(this, SubPosterMakeActivity.class, paths);
            });
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_POSTER) {
                Uri uri = data.getParcelableExtra(Extra.RET_URI);
                Glide.with(this).load(uri).into(mDataBinding.ivRet);
            }
        }
    }

    private void startPoster(ICallback callback) {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(2)
                .maxCount(6)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        if (callback != null) {
                            callback.onGetPhoto(pathList);
                        }
                    }
                });
    }

    private interface ICallback {

        void onGetPhoto(ArrayList<String> paths);

    }
}
